package com.um.jdy.platform.screen.manager;

import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.common.device.entity.vo.PileVO;
import com.um.jdy.common.device.service.PileService;
import com.um.jdy.platform.screen.entity.dto.PileStausSummaryDTO;
import com.um.jdy.platform.screen.entity.vo.HomeStationItemVO;
import com.um.jdy.platform.screen.entity.vo.PileListSummaryVO;
import com.um.jdy.platform.screen.entity.vo.PileStatusNumVO;
import com.um.jdy.platform.screen.entity.vo.PileStatusSummaryVO;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.ReflectUtil;
import com.um.springboot.starter.utils.StringUtil;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理类：充电枪表
 */
@Component
public class HomeManager extends BaseManager<PileService> {

    /**
     * 分页查询站场
     * @param params
     * @return
     */
    public Response<Page<HomeStationItemVO>> getStationPage(PageParams params){
        Page<HomeStationItemVO> page = new Page<>();
        page.setPageNo(params.getPageNo());
        page.setPageSize(params.getPageSize());

        params.getCondition().put("s.com_id",getComId());

        page = service.getCMSViewModelPageBySqlName(page, "get_station_page","get_station_count",HomeStationItemVO.class,params.getProperties(), params.getValues(),params.getRangeEntitys(),params.getOrderNames(),params.getSortOns());
        page.getResult().stream().forEach(stationItem -> {
            stationItem.setPileData(getListAndStatusSummary(stationItem.getDeviceStationId()));
        });
        return Response.success(page);
    }


    public Response<PileStatusSummaryVO> getStatusSummary(PileStausSummaryDTO dto){
        PileStatusSummaryVO summaryVO = new PileStatusSummaryVO();
        String[] properties = new String[]{"d.com_id","d.platform_merchant_id","d.device_station_id"};
        String[] values = new String[]{getComId(),StringUtil.getTrim(dto.getPlatformMerchantId(),"%"),StringUtil.getTrim(dto.getDeviceStationId(),"%")};
        List<PileStatusNumVO> list = service.getCMSViewModelListBySqlName("get_pile_status_summary",PileStatusNumVO.class,properties,values,
                new ArrayList<>(),new String[]{},new String[]{});
        for(PileStatusNumVO item : list){
            ReflectUtil.setValue(summaryVO,item.getDeviceStatus().toLowerCase(),item.getNum());
            summaryVO.setTotal(summaryVO.getTotal() + item.getNum());
        }

        return Response.success(summaryVO);
    }

    public PileListSummaryVO getListAndStatusSummary(String stationId){
        List<Pile> pileList = service.getList(new String[]{"device_station_id"},new Object[]{stationId},new String[]{"code"},new String[]{"asc"});

        PileListSummaryVO pileListSummaryVO = new PileListSummaryVO();
        List<PileStatusNumVO> statusList = service.getViewModelList("get_station_pile_status_summary",PileStatusNumVO.class,new Object[]{stationId,getComId()});
        for(PileStatusNumVO item : statusList){
            ReflectUtil.setValue(pileListSummaryVO,item.getDeviceType().toLowerCase()+item.getDeviceStatus(),item.getNum());
            pileListSummaryVO.setTotal(pileListSummaryVO.getTotal() + item.getNum());
        }

        pileListSummaryVO.setPileList(UMBeanUtils.copyBeanList(pileList,PileVO.class));
        return pileListSummaryVO;
    }

}
